package com.xjj.tree;

import java.util.ArrayList;
import java.util.List;

public class E0589NAryTreePreorderTraversal {

    List<Integer> result = new ArrayList<>();

    public List<Integer> preorder(Node root) {
        traverse(root);
        return result;
    }

    private void traverse(Node root) {
        if (root == null) {
            return;
        }
        result.add(root.val);
        if (root.children != null) {
            for (Node child : root.children) {
                traverse(child);
            }
        }
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    ;
}
